﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace Web.myadmin
{
    public partial class adminUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SessionHelp.AssertLogin(this); //登录验证

            #region 显示正确的布局
            string cmd = Request.QueryString["cmd"];
            showTheRightRegion(cmd);
            switch (cmd)
            {
                case "add": initAddForm();break;
                case "edit":initEditFrom();break;
                case "view": initViewTable(); break;
                case "delete": delete(); break;
                default: showInfoList(); break;
            }
            #endregion
        }

        #region 信息列表和查询
        /// <summary>
        /// 查询数据
        /// </summary>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            showInfoList();
        }

        /// <summary>
        /// 数据列表
        /// </summary>
        protected void showInfoList()
        {
            #region 变量定义
            DataSet dset = new DataSet();
            BZZ.DAL.adminUsers dal = new BZZ.DAL.adminUsers();
            System.Collections.Generic.List<System.Data.SqlClient.SqlParameter> paraList = new List<System.Data.SqlClient.SqlParameter>();
            string dataFields = ""; //读取的字段名，空为所有
            string strWhere = "1=1"; //查询子句
            string strOrderBy = " id desc"; //排序参数，如果需要分页，则必填 
            #endregion

            #region 查询条件
            //string strId = txtQryId.Text.Trim();
            //if (strId != "" && strId != null )
            //{
            //    strWhere += " and id=@id";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@id", strId));
            //} 
            string strUsername = txtQryUsername.Text.Trim();
            if (strUsername != null && strUsername != "" )
            {
                strWhere += " and username=@username";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@username", strUsername));
            } 
            string strPassword = txtQryPassword.Text.Trim();
            if (strPassword != null && strPassword != "" )
            {
                strWhere += " and password=@password";
                paraList.Add(new System.Data.SqlClient.SqlParameter("@password", strPassword));
            } 
            //string strRealname = txtQryRealname.Text.Trim();
            //if (strRealname != "" && strRealname != null )
            //{
            //    strWhere += " and realname=@realname";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@realname", strRealname));
            //} 
            //string strLevel = txtQryLevel.Text.Trim();
            //if (strLevel != "" && strLevel != null )
            //{
            //    strWhere += " and level=@level";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@level", strLevel));
            //} 
            //string strQq = txtQryQq.Text.Trim();
            //if (strQq != "" && strQq != null )
            //{
            //    strWhere += " and qq=@qq";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@qq", strQq));
            //} 
            //string strEmail = txtQryEmail.Text.Trim();
            //if (strEmail != "" && strEmail != null )
            //{
            //    strWhere += " and email=@email";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@email", strEmail));
            //} 
            //string strTelephone = txtQryTelephone.Text.Trim();
            //if (strTelephone != "" && strTelephone != null )
            //{
            //    strWhere += " and telephone=@telephone";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@telephone", strTelephone));
            //} 
            //string strLastloginTime = txtQryLastloginTime.Text.Trim();
            //if (strLastloginTime != "" && strLastloginTime != null )
            //{
            //    strWhere += " and lastloginTime=@lastloginTime";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@lastloginTime", strLastloginTime));
            //} 
            //string strLastloginip = txtQryLastloginip.Text.Trim();
            //if (strLastloginip != "" && strLastloginip != null )
            //{
            //    strWhere += " and lastloginip=@lastloginip";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@lastloginip", strLastloginip));
            //} 
            //string strLoginnum = txtQryLoginnum.Text.Trim();
            //if (strLoginnum != "" && strLoginnum != null )
            //{
            //    strWhere += " and loginnum=@loginnum";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@loginnum", strLoginnum));
            //} 
            //string strStatus = txtQryStatus.Text.Trim();
            //if (strStatus != "" && strStatus != null )
            //{
            //    strWhere += " and status=@status";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@status", strStatus));
            //} 
            //string strMemo = txtQryMemo.Text.Trim();
            //if (strMemo != "" && strMemo != null )
            //{
            //    strWhere += " and memo=@memo";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@memo", strMemo));
            //} 
            //string strExt1 = txtQryExt1.Text.Trim();
            //if (strExt1 != "" && strExt1 != null )
            //{
            //    strWhere += " and ext1=@ext1";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext1", strExt1));
            //} 
            //string strExt2 = txtQryExt2.Text.Trim();
            //if (strExt2 != "" && strExt2 != null )
            //{
            //    strWhere += " and ext2=@ext2";
            //    paraList.Add(new System.Data.SqlClient.SqlParameter("@ext2", strExt2));
            //} 
            #endregion

             #region 分页控件绑定
             int pageSize = 30; //分页每页记录个数
             int totalRecourd = 0; //总记录个数
             int currentPage = 1; //当前页码
             try { currentPage = int.Parse(Request.QueryString["p"]); }
             catch { }

             totalRecourd = dal.GetCount(strWhere, paraList);
             dset = dal.GetDatasetByPageSql(currentPage, pageSize, strWhere, strOrderBy, paraList, dataFields);
             if (dset!=null && dset.Tables[0].Rows.Count > 0)
             {
                 rptInfoList.DataSource = dset;
                 rptInfoList.DataBind();
                 litPager.Text = LIB.pager.getPageHtml(this, totalRecourd, pageSize);
             }
             else
             {
                 if (paraList.Count > 0)
                     litPromptMessage.Text = "没有查询到相关记录 ";
                 else
                 {
                     litPromptMessage.Text = " <a href='?cmd=add'>添加新纪录</a>";
                     divQuery.Visible = false;
                 }
             } 
             #endregion
             #region 单页数据绑定
             //strWhere += " " + strOrderBy;
             //dset = dal.GetDataset(strWhere, paraList, dataFields);
             //if (dset.Tables[0].Rows.Count > 0)
             //{
             //    rptInfoList.DataSource = dset;
             //    rptInfoList.DataBind();
             //}
             //else
             //{
             //    litPromptMessage.Text = "没有查询到相关记录";
             //} 
             #endregion

        }
        #endregion

        #region 添加、修改、删除操作
        /// <summary>
        /// 添加修改记录
        /// </summary>
        protected void btnSumit_Click(object sender, EventArgs e)
        {
            #region 获取数据
            bool isAdd = false;
            int varId ;	//id
            string varUsername;	//用户名
            string varPassword;	//密码
            string varRealname;	//真实姓名
            string varLevel;	//角色
            string varQq;	//qq
            string varEmail;	//邮箱
            string varTelephone;	//电话
            DateTime varLastloginTime ;	//最后一次登陆时间
            string varLastloginip;	//最后一次登陆ip
            int varLoginnum ;	//登陆次数
            int varStatus ;	//状态
            string varMemo;	//备注
            string varExt1;	//扩展1
            string varExt2;	//扩展2
            try
            {
               varId =int.Parse(txtId.Text.Trim());	//id
               varUsername =txtUsername.Text.Trim();	//用户名
               varPassword =txtPassword.Text.Trim();	//密码
               if (varPassword != "" && varPassword.Length < 32) varPassword =LIB.MD5Pwd.EPwd(varPassword).ToUpper();
               varRealname =txtRealname.Text.Trim();	//真实姓名
               varLevel =txtLevel.Text.Trim();	//角色
               varQq =txtQq.Text.Trim();	//qq
               varEmail =txtEmail.Text.Trim();	//邮箱
               varTelephone =txtTelephone.Text.Trim();	//电话
               varLastloginTime =DateTime.Parse(txtLastloginTime.Text.Trim());	//最后一次登陆时间
               varLastloginip =txtLastloginip.Text.Trim();	//最后一次登陆ip
               varLoginnum =int.Parse(txtLoginnum.Text.Trim());	//登陆次数
               varStatus =int.Parse(txtStatus.Text.Trim());	//状态
               varMemo =txtMemo.Text.Trim();	//备注
               varExt1 =txtExt1.Text.Trim();	//扩展1
               varExt2 =txtExt2.Text.Trim();	//扩展2
            }
            catch
            {
               LIB.JsWrite.Alert(this, "输入数据格式不合法，请检查输入数据。");
               return;
            }
            if (varId == 0) isAdd = true;
            #endregion

            #region 赋值model
            BZZ.DAL.adminUsers dal = new BZZ.DAL.adminUsers();
            BZZ.Model.adminUsers model = new BZZ.Model.adminUsers();
            if (isAdd)
            {
                model.username = varUsername;	//用户名
                model.password = varPassword;	//密码
                model.realname = varRealname;	//真实姓名
                model.level = varLevel;	//角色
                model.qq = varQq;	//qq
                model.email = varEmail;	//邮箱
                model.telephone = varTelephone;	//电话
                model.lastloginTime = varLastloginTime;	//最后一次登陆时间
                model.lastloginip = varLastloginip;	//最后一次登陆ip
                model.loginnum = varLoginnum;	//登陆次数
                model.status = varStatus;	//状态
                model.memo = varMemo;	//备注
                model.ext1 = varExt1;	//扩展1
                model.ext2 = varExt2;	//扩展2
                if (dal.Add(model) > 0)
                {
                    LIB.JsWrite.AlertAndGo(this, "添加成功", "?cmd=list");
                }
                else
                {
                    LIB.JsWrite.AlertAndGoback(this,"操作失败");
                }
            }
            else
            {
                model = dal.GetModel(varId);
                model.id = varId;	//id
                model.username = varUsername;	//用户名
                model.password = varPassword;	//密码
                model.realname = varRealname;	//真实姓名
                model.level = varLevel;	//角色
                model.qq = varQq;	//qq
                model.email = varEmail;	//邮箱
                model.telephone = varTelephone;	//电话
                model.lastloginTime = varLastloginTime;	//最后一次登陆时间
                model.lastloginip = varLastloginip;	//最后一次登陆ip
                model.loginnum = varLoginnum;	//登陆次数
                model.status = varStatus;	//状态
                model.memo = varMemo;	//备注
                model.ext1 = varExt1;	//扩展1
                model.ext2 = varExt2;	//扩展2
				if (dal.Update(model) > 0)
                {
                    LIB.JsWrite.Alert(this, "修改成功");
                }
                else
                {
                    LIB.JsWrite.AlertAndGoback(this,"操作失败");
                }
            }
  
            #endregion
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        protected void delete()
        {
            #region 删除操作
            int id = 0;
            try { id = int.Parse(Request.QueryString["id"]); }
            catch
            {
                //litPromptMessage.Text = "非法的参数";
                LIB.JsWrite.Alert(this, "非法的参数");
                return;
            }

            BZZ.DAL.adminUsers dal = new BZZ.DAL.adminUsers();
            if (dal.Delete(id)>0)
            {
                //litPromptMessage.Text = "删除成功";
                LIB.JsWrite.AlertAndGo(this, "删除成功", "?cmd=list");
            }
            else
            {
                //litPromptMessage.Text = "操作失败";
                LIB.JsWrite.AlertAndGo(this, "操作失败", "?cmd=list");
            } 
            #endregion
        }
        #endregion

        #region 初始化添加表单
        /// <summary>
        /// 初始化添加表单
        /// </summary>
        protected void initAddForm()
        {
            lblFormTitle.Text = "添加信息";
            if (!IsPostBack)
            {

                txtUsername.Text = "";
                txtPassword.Text = "";
                txtRealname.Text = "";
                txtLevel.Text = "";
                txtQq.Text = "";
                txtEmail.Text = "";
                txtTelephone.Text = "";
                txtLastloginTime.Text = "";
                txtLastloginip.Text = "";
                txtLoginnum.Text = "";
                txtStatus.Text = "";
                txtMemo.Text = "";
                txtExt1.Text = "";
                txtExt2.Text = "";
            }
        }
        #endregion

        #region 初始化修改表单
        /// <summary>
        /// 初始化修改表单
        /// </summary>
        protected void initEditFrom()
        {
            int id =Convert.ToInt32(Request.QueryString["id"]);
            lblFormTitle.Text = "修改信息";
            if (!IsPostBack)
            {
                BZZ.Model.adminUsers model = new BZZ.Model.adminUsers();
                BZZ.DAL.adminUsers dal = new BZZ.DAL.adminUsers();
                model = dal.GetModel(id);

                txtId.Text = model.id.ToString();
                txtUsername.Text = model.username.ToString();
                txtPassword.Text = model.password.ToString();
                txtRealname.Text = model.realname.ToString();
                txtLevel.Text = model.level.ToString();
                txtQq.Text = model.qq.ToString();
                txtEmail.Text = model.email.ToString();
                txtTelephone.Text = model.telephone.ToString();
                txtLastloginTime.Text = model.lastloginTime.ToString();
                txtLastloginip.Text = model.lastloginip.ToString();
                txtLoginnum.Text = model.loginnum.ToString();
                txtStatus.Text = model.status.ToString();
                txtMemo.Text = model.memo.ToString();
                txtExt1.Text = model.ext1.ToString();
                txtExt2.Text = model.ext2.ToString();
            }
        }
        #endregion

        #region 初始化查看表格
        /// <summary>
        /// 初始化查看表格
        /// </summary>
        protected void initViewTable()
        {
            int id = Convert.ToInt32(Request.QueryString["id"]);
            lblViewTitle.Text = "查看信息";
            if (!IsPostBack)
            {
                BZZ.Model.adminUsers model = new BZZ.Model.adminUsers();
                BZZ.DAL.adminUsers dal = new BZZ.DAL.adminUsers();
                model = dal.GetModel(id);

                lblUsername.Text = model.username.ToString();
                lblPassword.Text = model.password.ToString();
                lblRealname.Text = model.realname.ToString();
                lblLevel.Text = model.level.ToString();
                lblQq.Text = model.qq.ToString();
                lblEmail.Text = model.email.ToString();
                lblTelephone.Text = model.telephone.ToString();
                lblLastloginTime.Text = model.lastloginTime.ToString();
                lblLastloginip.Text = model.lastloginip.ToString();
                lblLoginnum.Text = model.loginnum.ToString();
                lblStatus.Text = model.status.ToString();
                lblMemo.Text = model.memo.ToString();
                lblExt1.Text = model.ext1.ToString();
                lblExt2.Text = model.ext2.ToString();
            }
        }

        #endregion

        #region 自定义函数

        /// <summary>
        /// 函数功能：示例函数
        /// 作者：buzhizhe 创建时间：2011.6.22
        /// 修改者：buzhizhe 最后修改时间：2011.6.22
        /// </summary>
        protected void YourFunction()
        {
            ///
        }


        #endregion


        #region 公共函数，不需要修改

        /// <summary>
        /// 根据cmd参数判断应该显示哪些部分内容
        /// </summary>
        /// <param name="cmd"></param>
        protected void showTheRightRegion(string cmd)
        {
            #region show or hide div
            switch (cmd)
            {
                case "add":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "edit":
                    divView.Visible = false;
                    divForm.Visible = true;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "view":
                    divView.Visible = true;
                    divForm.Visible = false;
                    divList.Visible = false;
                    divQuery.Visible = false;
                    break;
                case "delete":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    break;
                case "query":
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
                default:
                    divView.Visible = false;
                    divForm.Visible = false;
                    divList.Visible = true;
                    divQuery.Visible = true;
                    break;
            }
            #endregion
        }
        #endregion



    }


}

